package com.pengweiwei.androidzygote.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.account.AccountManager;
import com.pengweiwei.androidzygote.account.ui.LoginActivity;
import com.putong35.common.app.FileManager;
import com.putong35.common.baseactfrag.activity.BaseActivity;
import com.putong35.common.utils.SLog;
import com.putong35.common.utils.io.FileUtil;
import com.putong35.common.utils.zip.ZipExtractor;

import butterknife.BindView;

public class MainActivity extends BaseActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    @BindView(R.id.lytMain)
    LinearLayout lytMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     /*   TokenInfo info = new TokenInfo();
        info.setExpiresAt("");
        info.setAccessToken("AF318BE799982C97457092AEDA03CE4818724A13D80CF940E75D8AD2E24FB88ABD3165327F97AABB");
        info.setRefreshToken("AF318BE799982C97AECD790497EA6F4CD76F46F2ADECB9A9562CBD088F3E8D02E7A2A6D1E1419C0D");
        info.setServerTime("2016-07-21T14:43:41.080+0800");
        info.setMacKey("qjBDKRD6yJ");

        String url = "http://120.76.200.15:8888/uc/user";
        String authStr = Authorization.createAuthorization(info, url, "get");
        Log.e("vivi", "  auth string === " + authStr);*/

//        testGetAppAttrs();

        String path = FileManager.getWebCachePath(this) + "zygote_web_cache.zip";
        String toPath = FileManager.getWebCachePath(this) + "zygote_web_cache";
        FileUtil.copyAssetToSDCard(getAssets(), "www/zygote_web_cache.zip", path);

        ZipExtractor zipExtractor = new ZipExtractor(this,
                path, toPath,
                new ZipExtractor.OnExtractListener() {
                    @Override
                    public void onProgressUpdate(long currSize) {
                        SLog.e("onProgressUpdate currSize == " + currSize);
                    }

                    @Override
                    public void onComplete(long totalSize) {
                        SLog.e("onComplete totalSize == " + totalSize);
                    }

                    @Override
                    public void onFailure(int errorCode, long totalSize,
                                          long currSize) {
                        SLog.e("onFailure errorCode == " + errorCode + "totalSize" + totalSize);
                    }

                    @Override
                    public void onCaculateTotalSize(long totalSize) {
                        SLog.e("onCaculateTotalSize totalSize == " + totalSize);
                    }

                });
        zipExtractor.execute();
        statusTransucentAndLayoutFullscreen();

    }

    @Override
    public View createDataView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(R.layout.activity_main, null);
    }

    @Override
    protected void onResume() {
        super.onResume();

/*
        if (AccountManager.getAccount() == null) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            return;
        }*/

        String url = "file:///android_asset/www/test_tel.html";
        MainBottomFragment menuFragment = (MainBottomFragment) getSupportFragmentManager().findFragmentById(R.id.fragBottomMenu);
        hideToolbar();

      /*  Intent intent = new Intent(this, MyMvpListActivity.class);
        startActivity(intent);*/
    }


    public static final int REQ_SPLASH = 1;
    public static final int REQ_GUIDE = 2;
    public static final int REQ_LOGIN = 3;

    private void gotoMain() {
        if (AccountManager.getInstance().getAccount() == null) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivityForResult(intent, REQ_LOGIN);
        }
    }


    private long lastBackPressedTime = 0;

    @Override
    public void onBackPressed() {
        long currPressedTime = System.currentTimeMillis();
        // 如果两秒内连续点击back press
        if (currPressedTime - lastBackPressedTime <= 2000) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            lastBackPressedTime = currPressedTime;
        }
    }
}
